<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.sql.Connection.*" %>
<%@ page import="java.sql.ResultSet.*" %>
<%@ page import="java.sql.SQLException.*" %>
<%@ page import="java.sql.Statement.*" %>
<%@ page import="java.util.ArrayList.*" %>
<%@ page import="java.util.Collection.*" %>
<%@ page import="java.util.Iterator.*" %>
<%@ page import="org.apache.commons.logging.Log.*" %>
<%@ page import="org.apache.commons.logging.LogFactory.*" %>
<%@ page import="org.exolab.castor.jdo.Database.*" %>
<%@ page import="org.exolab.castor.jdo.PersistenceException.*" %>
<%@ page import="org.mbari.vars.dao.IDataObject.*" %>
<%@ page import="org.mbari.vars.dao.ObjectDAO.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.Concept.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.ConceptDelegate.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.ConceptName.*" %>
<%@ page import="org.mbari.vars.knowledgebase.model.dao.*" %>
<%@ page import="org.mbari.vars.dao.*" %>

<%
  String nameToFind = request.getParameter( "nameToFind" );
  if ( nameToFind != null ) {
    if ( nameToFind.equals( "null" ) ) {
      nameToFind = null;
    }
  }
  String searchName = request.getParameter( "searchName" );
  if ( searchName != null ) {
    if ( searchName.equals( "null" ) ) {
      searchName = null;
    }
  }
  if ( searchName == null ) {
      searchName = nameToFind;
  }
  String selectedFromName = null;
  String listName = null;
  String primaryName = null;
  String primaryImageURL = null;
  Collection<LWConceptName> matches = null;
  Set<LinkRealization> descInfo = null;
  String errorMessage;
  StringBuffer debugMessage = new StringBuffer();

  Vector prevFinds = (Vector) session.getAttribute( "prevFinds" );
  Vector sideBarVector = (Vector) session.getAttribute( "sideBarVector" );
  String nameToOpen = request.getParameter( "nameToOpen" );
  if ( nameToOpen != null ) {
    if ( nameToOpen.equals( "null" ) ) {
      nameToOpen = null;
    }
  }

%>

<html>

<head>
<title>Deep Sea Guide Concept View</title>

<script type="text/javascript" src="assets/javascripts/interior.js"></script>
<link rel="stylesheet" type="text/css" href="assets/styles/interior.css">
<link rel="stylesheet" type="text/css" href="styles/interiorVolc.css">
<link rel="stylesheet" type="text/css" href="assets/styles/print.css" media="print">

<%
   String kingdomName   = null; 
   String phylumName    = null;
   String className     = null;
   String subclassName  = null;
   String orderName     = null;
   String suborderName  = null;
   String familyName    = null;
   String subfamilyName = null;
   String genusName     = null;
   String speciesName   = null;


   String[] kingdomArray     = null; 
   String[] phylumArray      = null; 
   String[] classArray       = null; 
   String[] subclassArray    = null; 
   String[] orderArray       = null; 
   String[] suborderArray    = null; 
   String[] familyArray      = null; 
   String[] subfamilyArray   = null; 
   String[] genusArray       = null; 
   String[] speciesArray     = null; 

   try {

      selectedFromName = request.getParameter( "selectedFromName" );
      listName = null;
      errorMessage = null;
      debugMessage = new StringBuffer();

      if ( nameToFind == null ) {
        nameToFind = "eukaryota"; 
        if ( searchName != null ) {
          nameToFind = searchName; 
        }
      }
      debugMessage.append( "Finding: " + nameToFind );
      selectedFromName = request.getParameter( "selectedFromName" );
      debugMessage.append( "<br>selectedFromName: " + selectedFromName );
      if ( selectedFromName != null ) {
        session.setAttribute( selectedFromName + "Name", nameToFind );
      }
      if ( selectedFromName == null ) {
        listName = "kingdom"; 
        debugMessage.append( "<br>removing session variable kingdomName" );
        session.removeAttribute( "kingdomName" );
      } else
      if ( selectedFromName.equals( "kingdom" ) ) {
        session.setAttribute( "kingdomName", nameToFind );
        listName = "phylum"; 
        debugMessage.append( "<br>removing session variable phylumName" );
        session.removeAttribute( "phylumName" );
      } else
      if ( selectedFromName.equals( "phylum" ) ) {
        session.setAttribute( "phylumName", nameToFind );
        listName = "class"; 
        debugMessage.append( "<br>removing session variable className" );
        session.removeAttribute( "className" );
      } else
      if ( selectedFromName.equals( "class" ) ) {
        session.setAttribute( "className", nameToFind );
        listName = "subclass"; 
        debugMessage.append( "<br>removing session variable subclassName" );
        session.removeAttribute( "subclassName" );
      } else
      if ( selectedFromName.equals( "subclass" ) ) {
        session.setAttribute( "subclassName", nameToFind );
        listName = "order"; 
        debugMessage.append( "<br>removing session variable orderName" );
        session.removeAttribute( "orderName" );
      } else
      if ( selectedFromName.equals( "order" ) ) {
        session.setAttribute( "orderName", nameToFind );
        listName = "suborder"; 
        debugMessage.append( "<br>removing session variable suborderName" );
        session.removeAttribute( "suborderName" );
      } else
      if ( selectedFromName.equals( "suborder" ) ) {
        session.setAttribute( "suborderName", nameToFind );
        listName = "family"; 
        debugMessage.append( "<br>removing session variable familyName" );
        session.removeAttribute( "familyName" );
      } else
      if ( selectedFromName.equals( "family" ) ) {
        session.setAttribute( "familyName", nameToFind );
        listName = "subfamily"; 
        debugMessage.append( "<br>removing session variable subfamilyName" );
        session.removeAttribute( "subfamilyName" );
      } else
      if ( selectedFromName.equals( "subfamily" ) ) {
        session.setAttribute( "subfamilyName", nameToFind );
        listName = "genus"; 
        debugMessage.append( "<br>removing session variable genusName" );
        session.removeAttribute( "genusName" );
      } else
      if ( selectedFromName.equals( "genus" ) ) {
        session.setAttribute( "genusName", nameToFind );
        listName = "species"; 
        debugMessage.append( "<br>removing session variable speciesName" );
        session.removeAttribute( "speciesName" );
      } else
      if ( selectedFromName.equals( "species" ) ) {
        session.setAttribute( "speciesName", nameToFind );
        listName = "null";
      }
      debugMessage.append( "<br>listName: " + listName );
      if ( listName != null ) {
        session.setAttribute( listName, nameToFind );
      }
      Concept concept = null;
      primaryName = null;
      try {
        concept = ConceptDAO.getInstance().findByName( nameToFind );
      }
      catch (DAOException ex) {
        // database transaction failed. Handle Exception
        errorMessage = "ConceptDAO Error: " + ex.toString();
      }
      if (concept != null) {
        debugMessage.append( "<br>Found concept for " + nameToFind );
        primaryName = concept.getPrimaryConceptNameAsString( );

        descInfo = concept.getLinkRealizationSet();
        if ( descInfo != null ) {
          debugMessage.append( "<br>Linkrealization info success" );
        } else {
          debugMessage.append( "<br>Adding LinkRealization failed." );
        }
        primaryImageURL = concept.getPrimaryImage();
        if ( primaryImageURL != null ) {
          debugMessage.append( "<br>Primary image at" + primaryImageURL );
          primaryImageURL = primaryImageURL.replace( "sundog", "sundog.shore.mbari.org" );
        } else {
          debugMessage.append( "<br>Adding Media failed." );
        }
        debugMessage.append( "<br>Primary name is: " + primaryName );
/*
        Concept parentConcept = concept.getParentConcept();
        String parentName = parentConcept.getPrimaryConceptNameAsString();
        debugMessage.append( "<br>Parent: " + parentName );
*/

      }
    } catch (Exception ex) {
      errorMessage = "Test Error: " + ex.toString();
    }

%>

<SCRIPT SRC="mktree.js" LANGUAGE="JavaScript"></SCRIPT> 
<LINK REL="stylesheet" HREF="mktree.css">
</head>

<body>


<!--<%= debugMessage.toString() %>-->

<table align="center" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr><td colspan=3>
<!--begin whole page table-->
  <table id="pagetable" border="0" cellspacing="0" cellpadding="0" align="center">
  <!--begin header-->
  <tr>
	<td id="header_1" colspan="3" align="center" valign="top"><table id="toplinks" border="0" width="100%" cellspacing="0" cellpadding="0">	
        <tr bgcolor="#336699">
	  <td colspan="9" align="center" valign="top" height="6"><img src="http://www.mbari.org/art/interior_levels/header/header_top.gif" alt="" width="750" height="6" hspace="0" vspace="0" border="0"></td>
        </tr>

        <tr bgcolor="#E1E2D5">
	  <td align="left" valign="top" height="19"><img src="http://www.mbari.org/art/interior_levels/header/links/spacer_left.gif" alt="" width="8" height="19" hspace="0" vspace="0" border="0"></td>
	  <td align="center" valign="top"><a href="/default.htm" onMouseOver="imgOn('link1'); self.status='Return to MBARI homepage'; return true;" onMouseOut="imgOff('link1'); self.status='';"><img src="http://www.mbari.org/art/interior_levels/header/links/home_off.gif" alt="Home" width="85" height="19" hspace="0" vspace="0" border="0" name="link1"></a></td>
	  <td align="center" valign="top"><a href="/news/" onMouseOver="imgOn('link2'); self.status='News and Information about MBARI'; return true;" onMouseOut="imgOff('link2'); self.status='';"><img src="http://www.mbari.org/art/interior_levels/header/links/news_info_off.gif" alt="News and Information" width="126" height="19" hspace="0" vspace="0" border="0" name="link2"></a></td>
	  <td align="center" valign="top"><a href="/moos/" onMouseOver="imgOn('link3'); self.status='Ocean Observatories'; return true;" onMouseOut="imgOff('link3'); self.status='';"><img src="http://www.mbari.org/art/interior_levels/header/links/observatories_off.gif" alt="Ocean Observatories" width="138" height="19" hspace="0" vspace="0" border="0" name="link3"></a></td>
	  <td align="center" valign="top"><a href="/rd/" onMouseOver="imgOn('link4'); self.status='Research and Development'; return true;" onMouseOut="imgOff('link4'); self.status='';"><img src="http://www.mbari.org/art/interior_levels/header/links/r_d_off.gif" alt="Research and Development" width="87" height="19" hspace="0" vspace="0" border="0" name="link4"></a></td>
	  <td align="center" valign="top"><a href="/data/" onMouseOver="imgOn('link5'); self.status='Scientific Data and Images'; return true;" onMouseOut="imgOff('link5'); self.status='';"><img src="http://www.mbari.org/art/interior_levels/header/links/data_off.gif" alt="Data" width="79" height="19" hspace="0" vspace="0" border="0" name="link5"></a></td>
	  <td align="center" valign="top"><a href="/dmo/" onMouseOver="imgOn('link6'); self.status='Marine Operations'; return true;" onMouseOut="imgOff('link6'); self.status='';"><img src="http://www.mbari.org/art/interior_levels/header/links/marine_ops_off.gif" alt="Marine Ops" width="122" height="19" hspace="0" vspace="0" border="0" name="link6"></a></td>
	  <td align="center" valign="top"><a href="/search.htm" onMouseOver="imgOn('link7'); self.status='Search the MBARI website'; return true;" onMouseOut="imgOff('link7'); self.status='';"><img src="http://www.mbari.org/art/interior_levels/header/links/search_off.gif" alt="Search" width="92" height="19" hspace="0" vspace="0" border="0" name="link7"></a></td>
	  <td align="right" valign="top"><img src="http://www.mbari.org/art/interior_levels/header/links/spacer_right.gif" alt="" width="13" height="19" hspace="0" vspace="0" border="0"></td>
        </tr>
  </table>
</td></tr>

<tr>
	<td id="header_2" align="right" valign="top" rowspan="2"><img src="http://www.mbari.org/art/interior_levels/header/mbari_logo_left.gif" alt="" width="140" height="95" hspace="0" vspace="0" border="0"></td>
	<td bgcolor=#fff0eb id="header_3" align="left" valign="top" rowspan="2"><img src="art/interior_levels/header/mbari_logo_right.gif" alt="" width="95" height="95" hspace="0" vspace="0" border="0"></td>
	<td id="header_4" align="right" valign="top" height="42"><img src="art/interior_levels/header/info_field/blank.gif" alt="" width="505" height="42" hspace="0" vspace="0" border="0" name="infofield"></td>
</tr>


<div id="pagebreak" align="center"><br></div>
<tr>
	<td align="right" valign="top" height="53" width=100%>
          <!--provide section title here -->
          <div id="sectiontitle">Deep Sea Guide</div>     
        </td>
</tr>
<!--end header-->

<!--begin body row-->
<form action=DeepSeaGuide.jsp type=get>
<tr>
<!--navbar cell-->
	<td id="navbar_cell" width="150" align="left" valign="top">
	
<!-- *** Server include for volcanism navbar *** -->
<!--navbar section title -->
  <div id="navtitle"><a class="navtitle">Deep&nbsp;Sea&nbsp;Guide</a></div>
  <br>
<!--begin main navbar links-->

<!--begin content cell-->
    <td id="content_cell" align="left" valign="top" colspan="2">
    

<!-- *** begin page content here *** -->
<div id="content" align="left">
<!--/div at the end of the content part-->

<div id="pagebreak" align="center"><br></div>
<!-- *** Server include for header links *** -->
<!--header links-->
<!--unorderlists here-->
<div id="pagetitle" align="center">Deep Sea Guide for <%= nameToFind %></div>

<table width=100% BORDER=0 RULES=NONE FRAME=BOX>
<tr>
<td align=center>
  <% if ( primaryImageURL != null ) { %>
    <img src=<%= primaryImageURL %> width=200px>
  <% } else { %>
    <br>No images available
  <% } %>
</td>
</tr>
<tr>
<td>
<% if ( descInfo != null ) { %>
<!--
  LinkRealization set for <%= nameToFind %> contains <%= descInfo.size() %> elements
//-->
  <ul>
  <% for( LinkRealization info : descInfo ) { %>
    <% String infoStr = info.toString().replace( " | ", ":" ); %>
    <% if ( infoStr == null ) { %>
      <li><%= "infoStr is null" %></li>
    <% } else { %>
      <% String[] infoArray = infoStr.split( ":", 0 ); %>
      <% if ( infoArray.length != 3 ) { %>
        <li><%= "infoArray has " + infoArray.length + " elements" %></li>
      <% } else { %>
      <li>
        <%= infoArray[0].replace( "dsg-", "" ) %>:&nbsp
        <% if ( infoArray[2].indexOf( "http://" ) > -1 ) { %>
          <a target=_blank href=<%= infoArray[2].substring( infoArray[2].indexOf( "http://") ) %>>
            <%= infoArray[2] %>
          </a>
        <% } else { %>
          <%= infoArray[2] %>
        <% } %>
      </li>
    <% } %>
    <% } %>
  <% } %>
  </ul>
<% } %>


</td>
</tr>
</table>
</td>
</tr>
</table>


<% if ( errorMessage != null ) { %>
  <font color=red size=5><%= errorMessage %></font>
<% } %>

<!--sand colored horizontal rule-->
<div align="center" class="sand_line"></div>


<!-- *** end page content *** -->

</div>
</td></tr>
<!--end body row-->

<!--begin footer-->
<tr>
	<td id="footer" colspan="3" align="right" valign="top"><img src="http://www.mbari.org/art/interior_levels/footer/interior_footer.gif" alt="" width="750" height="50" hspace="0" vspace="0" border="0" usemap="#footer_map">

<!--2nd level footer map-->
<map name="footer_map">
	<!--footer - home-->
<area shape="rect" coords="19,16,62,35" href="/default.htm" alt="Home" onMouseOver="self.status='Return to MBARI homepage'; return true;" onMouseOut="self.status='';">

	<!--footer - news and info-->
<area shape="rect" coords="90,16,172,35" href="/news/" alt="News and Information" onMouseOver="self.status='News and Information'; return true;" onMouseOut="self.status='';">

	<!--footer - observatories-->
<area shape="rect" coords="199,16,295,35" href="/moos/" alt="Observatories" onMouseOver="self.status='Ocean Observatories'; return true;" onMouseOut="self.status='';">

	<!--footer - research and development-->
<area shape="rect" coords="322,16,366,35" href="/rd/" alt="Research and Development" onMouseOver="self.status='Research and Development'; return true;" onMouseOut="self.status='';">

	<!--footer - data and images-->
<area shape="rect" coords="509,16,544,35" href="/data/" alt="Data and Images" onMouseOver="self.status='Data and Images'; return true;" onMouseOut="self.status='';">

	<!--footer - canyon head-->
<area shape="rect" coords="576,16,654,35" href="/dmo/" alt="Marine Ops" onMouseOver="self.status='Marine Operations'; return true;" onMouseOut="self.status='';">

	<!--footer - search-->
<area shape="rect" coords="682,16,731,35" href="/search.htm" alt="Search" onMouseOver="self.status='Search the MBARI website.'; return true;" onMouseOut="self.status='';">
</map></td>
</tr>

<!--end footer-->
</table>

&nbsp; <p>

<script>
  addEvent(window,"load",expandToItem('tree1','<%= nameToFind %>');
</script>
</body>
</html>
